import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { ReportConfigService } from '../report-config.service'
import { DialogService } from 'primeng/api';
import { QkPartSelectorComponent } from '../../../common/component/part/qkpart.component';

@Component({
    selector: 'report-config-authfilter',
    templateUrl: './report-config-authfilter.component.html',
    styleUrls: ['./report-config-authfilter.component.scss']
})
export class ReportConfigAuthFilterComponent implements OnInit {
    currentReportId: number = 0;
    cols: any[] = [
        { header: '编码', field: 'partId', style: { 'width': '8%', 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '名称', field: 'partName', style: { 'width': '15%', 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '类型', field: 'partType', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: '机构编码', field: 'unitCode', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: 'SQL过滤', field: 'filterSql', style: { 'width': '45%', 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } }
    ];
    authFilters: any[] = [
    ];
    @Input()
    set reportId(report: number) {
        if (report) {
            this.currentReportId = report;
            this.reportConfigService.listReportAuthFilter(this.currentReportId).subscribe(data => this.authFilters = data);
        }
    }
    get reportId(): number { return this.currentReportId; }

    constructor(
        private fb: FormBuilder,
        private reportConfigService: ReportConfigService,
        private dialogService: DialogService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) {
    }

    ngOnInit() {
    }

    /**添加查询条件 */
    addAuthFilters() {
        const ref = this.dialogService.open(QkPartSelectorComponent, {
            header: '请选择参与者',
            width: '420px',
            contentStyle: { 'height': '550px' },
            data: {
                'authorityCode': 2
            }
        });

        ref.onClose.subscribe((data: any) => {
            let temp = [];
            this.authFilters.forEach(ele => temp.push(ele.partId))
            data.forEach(ele => {
                if ($.inArray(ele.id, temp) == -1) {
                    this.authFilters.push({ 'reportId': this.currentReportId, 'partId': ele.id, 'partName': ele.label, 'partType': ele.data['type'], 'unitCode': ele.parent.data.code });
                }
            });
        })
    }

    /** 删除条件 */
    deleteAuthFilter(rowData) {
        this.mdlDialogService.confirm('您确定要删除选中的权限信息吗？', "取消", "确定").subscribe(() => {
            this.reportConfigService.deleteReportAuthFilter(rowData.reportId, rowData.partId).subscribe(data => {
                this.reportConfigService.listReportAuthFilter(this.currentReportId).subscribe(data => this.authFilters = data);
                this.mdlSnackbarService.showToast('删除成功');
            });
        });
    }

    /** 保存条件 */
    saveAuthFilters() {
        this.reportConfigService.updateReportAuthFilters(this.authFilters).subscribe(data => this.mdlSnackbarService.showToast('保存成功'));
    }
}
